<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class FriController extends Controller
{
   
   //---------------------------------------------用户添加
    //显示用户添加页面
     public function getAdd()
     {
         return  view('admin.fri.add');
   
     } 


     //执行用户添加
     public function postInsert(Request $request)
     {

         //dd($request->all());
        $this->validate($request,[

             'title' => 'required|max:25',
             'content' => 'required|max:255',
            ],[
               'title.required' => '标题不能为空',
               'title.max' => '标题过长了',
               'content.required' => '内容不能为空',
               'content.max' => '内容过长了',
            ]);
            //dd($request->all());
            //dd($request->hasFile('img'));
            if($request->hasFile('img')){
            //随机文件名
            $name = time()+rand(1,99);

             //获取后缀名
            $ext = $request->file('img')->getClientOriginalExtension();
            echo $name.'.'.$ext;
            //检测后缀名
            $arr = array('jpg','png','jpeg');
            //判断
            if(!in_array($ext,$arr)){
                echo '<script type="text/javascript">
                    alert("上传文件不合法");
                    location.href="http://la.cn/test/add";
                </script>';
            }
            $request->file('img')->move('./uploads/',$name.'.'.$ext);
            //echo '<img src="/uploads/'.$name.'.jpg" alt="">';
            
            $img = self::suofang('./uploads/'.$name.'.'.$ext,129,110);
        }
     

        //上面验证成功 执行下面
        $data = $request->only(['title','content','img']);
        $data['img'] = $name.'.'.$ext;
         //dd($data);
        //执行插入
        $res =  DB::table('fri')->insert($data);
        //dd($res);
         if($res){
            return redirect('/admin/fri/list')->with('success','链接添加成功');

        }else{
             return back()->with('error','链接添加失败');
        }  
     }
      //显示用户列表
        public function getList(Request $request)
        {

            // $fri = DB::table('fri')->get();
            $fri = DB::table('fri')->paginate(5);
            // var_dump($fri);
           
             if($request->input('keywords')){
                $fri = DB::table('fri')->where('title','like','%'.$request->input('keywords').'%')->paginate(5);
                    }else{
                    $fri = DB::table('fri')->paginate(5);
                }
            // dd($fri);
            return view('admin.fri.list',['fri'=>$fri]);
        }


        //执行修改
        public function postUpdate(Request $request)
        {
            //dd($request->all());
            $id = $request->only('id');
            $data = $request->only('title','content','img');

            //执行
             $res = DB::table('fri')->where('id',$id)->update($data);
              // var_dump($res);
               if($res){
                    return redirect('/admin/fri/list')->with('success','用户修改成功');

                }else{
                     return back()->with('error','用户修改失败');
                }
        }


        //显示修改页面
        public function getEdit($id)
        {
            
            $fri = DB::table('fri')->where('id',$id)->first();

            return view('admin.fri.edit',['fri'=>$fri]);
        }

        //执行用户删除

        public function getDelete($id)

        {
            
                   
                $res = DB::table('fri')->where('id',$id)->delete();
                 if($res){
                    return redirect('/admin/fri/list')->with('success','用户删除成功');

                }else{
                     return back()->with('error','用户删除失败');
                }
        }



   //缩放函数
        public static function suofang($spath, $sw = 100, $sh = 100,$pre = 's_')
                {
                    //1.创建画布
                    //计算比例  100 100  要缩放的图像的宽高  1920 1200  //用小的比例
                    $srcinfo = getimagesize($spath);
                    //根据图片类型 去创建图像资源 ...
                    $src = imagecreatefromjpeg($spath); //strrchr  aaaa.jpg
                    
                    if($sw/$srcinfo[0] > $sh/$srcinfo[1]){
                        $bili = $sh/$srcinfo[1];
                    }else{
                        $bili = $sw/$srcinfo[0];
                    }
                    //舍去法取整
                    $sw = floor($srcinfo[0] * $bili);
                    $sh = floor($srcinfo[1] * $bili);


                    $dst = imagecreatetruecolor($sw,$sh);

                    //3.开始绘画
                    imagecopyresampled($dst, $src, 0, 0, 0, 0, $sw, $sh, $srcinfo[0], $srcinfo[1]);

                    //5.将图像保存为文件
                    //随机文件名  //获取文件后缀名  目录
                    //$ext = strrchr($spath,'.');
                    //$filename = time().rand(1000,9999).$ext;
                    $filename = rtrim(dirname($spath),'/').'/'.$pre.basename($spath);
                    
                    imagejpeg($dst, $filename);

                    //6.销毁资源
                    imagedestroy($dst);
                    imagedestroy($src);
                    
                    //返回原文件名加前缀
                    return $pre.basename($spath);
                }



	
}
